﻿using MPic.Business.DAL;
using MPic.Business.Entity;
using MPic.Business.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MPic.Business.BLL
{
    public class EmailBLL
    {
        private EmailDAL emailDal = new EmailDAL(DbConnectionEnum.MPic);
        private static object locker = new object();
        private static EmailBLL _instance;
        public static EmailBLL Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (locker)
                    {
                        if (_instance == null)
                        {
                            _instance = new EmailBLL();
                        }
                    }
                }
                return _instance;
            }
        }

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="reciver"></param>
        /// <param name="code"></param>
        public void Send(int userId, string reciver, string code)
        {
            var email = new EmailEntity
            {
                UserId = userId,
                Title = "[MPic图床]注册验证",
                Reciver = reciver,
                CreateTime = DateTime.Now,
                SendTime = DateTime.Now,
                SendFlag = true,
                VerifyCode = code
            };
            emailDal.Insert(email);
        }

        /// <summary>
        /// 校验验证码
        /// </summary>
        /// <param name="email"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public bool CheckVarifyCode(string email, string code)
        {
            var mail = emailDal.Get(new { Reciver = email, VerifyCode = code }, "SendTime DESC");
            if (mail != null && mail.SendTime > DateTime.Now.AddMinutes(-10))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 校验发送时间
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public bool CheckSendTime(string email)
        {
            var mail = emailDal.Get(new { Reciver = email }, "SendTime DESC");
            if ((mail != null && mail.SendTime < DateTime.Now.AddMinutes(-1)) || mail == null)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 发送次数校验
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public bool CheckSendNum(string email)
        {
            DateTime dt = DateTime.Now.AddHours(-24);
            var param = new { Email = email, SendTime = dt };
            var count = emailDal.RecordCount(" Reciver=@Email AND SendTime>@SendTime", param);
            if (count > 2)
            {
                return false;
            }
            return true;
        }
    }
}
